package com.library.rest.utils;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-07-09 09:30
 * @version: 9.1.0
 */
public class Uttid {
    protected static final String PREFS_FILE = "device_id.xml";
    protected static final String PREFS_DEVICE_ID = "device_id";
    protected static UUID uuid;

    public static String getUttid(Context context) {
        if (uuid == null) {
            init(context);
        }
        return uuid.toString();
    }

    public static void init(Context context) {

        if (uuid == null) {
            synchronized (Uttid.class) {
                if (uuid == null) {
                    final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);
                    final String id = prefs.getString(PREFS_DEVICE_ID, null);

                    if (id != null) {
                        // Use the ids previously computed and stored in the prefs file
                        uuid = UUID.fromString(id);
                    } else {
                        final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

                        // Use the Android ID unless it's broken, in which case fallback on deviceId,
                        // unless it's not available, then fallback on a random number which we store
                        // to a prefs file
                        try {
                            if (!"9774d56d682e549c".equals(androidId)) {
                                uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                            } else {
                                String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
                                if (TextUtils.isEmpty(deviceId)) {
                                   deviceId = android.os.Build.SERIAL;
                                }
                                uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
                            }
                        } catch (UnsupportedEncodingException e) {
                            uuid = UUID.randomUUID();
                        }

                        // Write the value out to the prefs file
                        prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).commit();

                    }
                }
            }
        }

    }

//    private static String getUttid() {
//        return UUID.randomUUID().toString();
//    }
//
//    private static String getAndroidId() {
//        final String androidId = Settings.Secure.getString(ContextUtils.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
//        Log.e("zhiwei:", " getAndroidId: androidId=" + androidId);
//        return androidId;
//    }
//
//    private static String getSerialNumber() {
//        String serialNumber = android.os.Build.SERIAL;
//        Log.e("zhiwei:", " getSerialNumber: serialNumber=" + serialNumber);
//        return serialNumber;
//    }

}
